

import Player from "../../操作杆/scripts/Player";
import { EventStepCtrl } from "./EventStepCtrl";
import { z } from "./z";
// import SceneMap from "../SceneMap";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameCtrl extends cc.Component {
    /**当前运行的step */
    @property(EventStepCtrl)
    public currentStep: EventStepCtrl = null;

    /**说话节点 */
    @property(cc.Node)
    public talkNode: cc.Node = null;

    /**目标文本组件 */
    @property(cc.Label)
    public targetLabel: cc.Label = null;

    @property(Player)
    public player: Player = null;

    /**是否开始控制 */
    private isStartCtrl: boolean = false;

    onLoad() {
        z.ctl = this;
        z.currentStep = this.currentStep;
        z.player = this.player;

        //开启碰撞检测
        var manager = cc.director.getCollisionManager();
        manager.enabled = true;
        manager.enabledDebugDraw = true;
    }

    /**开始 */
    public startGame(): void {
        this.isStartCtrl = true;
        EventStepCtrl.setStepLevelIndex(this.currentStep);
        this?.currentStep.showStep();
    }


    /**处理点击事件 */
    public dealClickEvent(location: cc.Vec2) {
        // if (this.currentStep) {
        //     let isClick = this.currentStep.dealClickEvent(this, location)
        //     this.checkFinishStep();
        //     return isClick;
        // }
        // return false;
    }


    private checkFinishStep(): void {
        // if(this.currentStep.checkFinishStep(this.scenMap.player))
        // {
        //     if(this.currentStep.isGameOverStep())
        //     {
        //         this.node.dispatchEvent( new cc.Event.EventCustom('GameOver', true));
        //     }
        //     else if(this.currentStep.isGameFailStep())
        //     {
        //         let event = new cc.Event.EventCustom('GameFail', true);
        //         event.setUserData(this.currentStep.failDes);
        //         this.node.dispatchEvent( event);
        //     }

        //     this.switchNextStep(this.currentStep.nextStep);

        // }

    }

    private switchNextStep(nxtStep: EventStepCtrl) {
        if (!nxtStep) {
            return;
        }

        this.currentStep = nxtStep;
        if (this.currentStep.isGameStartStep()) {
            this.node.dispatchEvent(new cc.Event.EventCustom('GameStart', true));
        }

        // if (this.currentStep.canTouchCtrlPlay()) {
        //     this.node.dispatchEvent(new cc.Event.EventCustom('TouchMovePlay', true));
        // }

        if (this.currentStep.targetDes.length > 0) {
            this.targetLabel.string = this.currentStep.targetDes;
        }

        this.currentStep.showStep();
    }

    /**
     * 跳到指定事件
     * @param nxtStep 下一个事件
     */
    public gotoStep(nxtStep: EventStepCtrl) {
        this.currentStep.finishStep();
        this.switchNextStep(nxtStep);
    }

    public canDealClickEvent(): boolean {
        if (!this.currentStep) {
            return false;
        }

        // return this.currentStep.canDealClickEvent();
    }

    public canTouchCtrlPlay(): boolean {
        if (!this.currentStep) {
            return true;
        }

        // return this.currentStep.canTouchCtrlPlay();
    }


    update(dt) {
        if (!this.isStartCtrl) {
            return;
        }

        if (this.currentStep) {
            // if(this.currentStep.dealChacterMove(this.scenMap.player))
            // {
            // }
            this.checkFinishStep();
        }
    }
}
